// -----------------------------------------------------------------------------
// M A R I T I M E   R E S E A R C H   I N S T I T U T E   N E T H E R L A N D S
// -----------------------------------------------------------------------------
// Program  :
// Module   :
// File     : GtkAppWindow.cpp
// -----------------------------------------------------------------------------
// Revisions:
// February 13, 2009  asicherer
// -----------------------------------------------------------------------------

#include <sys/time.h>
#include "GtkAppWindow.h"
#include "assertplus.h"
 #include <X11/Xlib.h>

// -----------------------------------------------------------------------------
// function GtkAppWindow::GtkAppWindow
// -----------------------------------------------------------------------------
/// constructor
/** initializes gtk system, loads interface description and displays window */
// -----------------------------------------------------------------------------
GtkAppWindow::GtkAppWindow(int *argc,             ///< from main
                           char ***argv,          ///< from main
                           const char *xmlFile,   ///< gui description
                           const char *windowName)///< name of main window
    : m_pBuilder(0)
    , m_pWindow(0)
{
    // initialize Gtk window system
 XInitThreads();
    gtk_init(argc, argv);

    // read interface definition
    m_pBuilder = gtk_builder_new();
    gtk_builder_add_from_file(m_pBuilder, xmlFile, NULL);

    // main window
    m_pWindow = GTK_WIDGET(gtk_builder_get_object(m_pBuilder, windowName));
    ASSERTPOINTER(m_pWindow);

    // connect widgets with actions
    gtk_builder_connect_signals(m_pBuilder, NULL);

    // display window
    show();
}


// -----------------------------------------------------------------------------
// function GtkAppWindow::~GtkAppWindow
// -----------------------------------------------------------------------------
/// destructor
/** frees resources */
// -----------------------------------------------------------------------------
GtkAppWindow::~GtkAppWindow()
{
    g_object_unref(G_OBJECT(m_pBuilder));
}


// -----------------------------------------------------------------------------
// function GtkAppWindow::show
// -----------------------------------------------------------------------------
/// displays window and handles any pending events
// -----------------------------------------------------------------------------
void GtkAppWindow::show()
{
    gtk_widget_show(m_pWindow);
    handleEvents();
}


// -----------------------------------------------------------------------------
// function GtkAppWindow::handleEvents
// -----------------------------------------------------------------------------
/// handles any pending events
// -----------------------------------------------------------------------------
void GtkAppWindow::handleEvents()
{
    while (gtk_events_pending())
    {
        gtk_main_iteration();
    }
}


// -----------------------------------------------------------------------------
// function messageBox
// -----------------------------------------------------------------------------
/// general function to display a Gtk message in a box with an OK button
// -----------------------------------------------------------------------------
void messageBox(const char *title, const char *message)
{
    GtkWidget *dialog = gtk_dialog_new_with_buttons(title,
                                                    NULL,
                                                    GTK_DIALOG_MODAL,
                                                    GTK_STOCK_OK,
                                                    GTK_RESPONSE_NONE,
                                                    NULL);
    GtkWidget *label = gtk_label_new(message);
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);
    gtk_widget_show_all(dialog);
    gtk_dialog_run((GtkDialog*)dialog);
    gtk_widget_destroy(dialog);
}


// -----------------------------------------------------------------------------
// function confirmBox
// -----------------------------------------------------------------------------
/// general function to display a Gtk question in a box with OK/Cancel buttons
// -----------------------------------------------------------------------------
bool confirmBox(const char *title, const char *message)
{
    GtkWidget *dialog = gtk_dialog_new_with_buttons(title,
                                                    NULL,
                                                    GTK_DIALOG_MODAL,
                                                    GTK_STOCK_OK,
                                                    GTK_RESPONSE_OK,
                                                    GTK_STOCK_CANCEL,
                                                    GTK_RESPONSE_CANCEL,
                                                    NULL);
    GtkWidget *label = gtk_label_new(message);
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);
    gtk_widget_show_all(dialog);
    gint result = gtk_dialog_run((GtkDialog*)dialog);
    gtk_widget_destroy(dialog);
    return result == GTK_RESPONSE_OK;
}


// -----------------------------------------------------------------------------
// function yesnoBox
// -----------------------------------------------------------------------------
/// general function to display a Gtk question in a box with Yes/No/Cancel buttons
// -----------------------------------------------------------------------------
bool yesnoBox(const char *title, const char *message, bool *yes)
{
    GtkWidget *dialog = gtk_dialog_new_with_buttons(title,
                                                    NULL,
                                                    GTK_DIALOG_MODAL,
                                                    GTK_STOCK_YES,
                                                    GTK_RESPONSE_YES,
                                                    GTK_STOCK_NO,
                                                    GTK_RESPONSE_NO,
                                                    GTK_STOCK_CANCEL,
                                                    GTK_RESPONSE_CANCEL,
                                                    NULL);
    GtkWidget *label = gtk_label_new(message);
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);
    gtk_widget_show_all(dialog);
    gint result = gtk_dialog_run((GtkDialog*)dialog);
    gtk_widget_destroy(dialog);
    *yes = result == GTK_RESPONSE_YES;
    return result != GTK_RESPONSE_CANCEL;
}




